﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Entity
{
    /// <summary>
    /// API接口集类
    /// </summary>
    [XmlRoot("Api接口")]
    public class ApiInterfaceSetV2d0
    {
        /// <summary>
        /// 项目名称
        /// </summary>
        [XmlElement("项目名称")]
        public string ProjectName { get; set; }
        /// <summary>
        /// 接口集
        /// </summary>
        [XmlArray("接口集"), XmlArrayItem("接口")]
        public List<ApiInterfaceV2d0> ApiInterfaceList { get; set; }
        public ApiInterfaceSetV2d0()
        {
            ProjectName = "默认项目";
            ApiInterfaceList = new List<ApiInterfaceV2d0>();
        }
        /// <summary>
        /// 读取xml文件得到接口集
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ApiInterfaceSetV2d0 ReadFromFile(string fileName)
        {
            ApiInterfaceSetV2d0 result = null;
            try
            {
                if (File.Exists(fileName))
                {
                    StreamReader sR = new StreamReader(fileName);
                    XmlSerializer xmlS = new XmlSerializer(typeof(ApiInterfaceSetV2d0));
                    result = (ApiInterfaceSetV2d0)xmlS.Deserialize(sR);
                    sR.Close();
                }
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
            return result;
        }
        /// <summary>
        /// 将接口集类,存储为xml文件
        /// </summary>
        /// <param name="interfaceSet">接口集</param>
        /// <param name="filename">xml文件名</param>
        public static void SaveToFile(ApiInterfaceSetV2d0 interfaceSet, string filename)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ApiInterfaceSetV2d0));
            StreamWriter sW = new StreamWriter(filename);
            try
            {
                xmlSerializer.Serialize(sW, interfaceSet);
            }
            finally
            {
                sW.Close();
            }
        }

    }
    /// <summary>
    /// Api接口
    /// </summary>
    [XmlRoot("接口")]
    public class ApiInterfaceV2d0
    {
        /// <summary>
        /// 接口名称
        /// </summary>
        [XmlElement("InterfaceName")]
        public string InterfaceName { get; set; }
        /// <summary>
        /// 接口描述
        /// </summary>
        [XmlElement("InterfaceDescribe")]
        public string InterfaceDescribe { get; set; }
        /// <summary>
        /// 接口url
        /// </summary>
        [XmlElement("InterfaceUrl")]
        public string InterfaceUrl { get; set; }
        /// <summary>
        /// 接口类型
        /// </summary>
        [XmlElement("InterfaceType")]
        public string InterfaceType { get; set; }
        /// <summary>
        /// 接口消息体模板
        /// </summary>
        [XmlElement("MessageBody")]
        public string MessageBody { get; set; }
        public ApiInterfaceV2d0()
        {
            InterfaceName = string.Empty;
            InterfaceDescribe = string.Empty;
            InterfaceUrl = string.Empty;
            InterfaceType = "get";
            MessageBody = string.Empty;
        }
        public ApiInterfaceV2d0(string name, string describe, string url, string messageBody, string type = "get")
        {
            InterfaceName = name;
            InterfaceDescribe = describe;
            InterfaceUrl = url;
            InterfaceType = type;
            MessageBody = messageBody;

        }
    }
}
